<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="会议名称" prop="meetingName">
        <el-input
          v-model="queryParams.meetingName"
          placeholder="请输入会议名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会议时间" >
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="所属场站" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入所属场站"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5" v-if="station_btn">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:meeting:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="meetingList" :max-height="tableHeight" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="会议名称" align="left" prop="meetingName" />
      <el-table-column label="会议时间" align="center" prop="meetingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.meetingTime) }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="周几" align="center" prop="meetingWeek" />-->
      <el-table-column label="会议地点" align="left" prop="meetingPlace" :show-overflow-tooltip="true" />
      <el-table-column label="主持人" align="center" prop="meetingHost" />
      <el-table-column label="记录人" align="center" prop="meetingNoteTaker" />
      <el-table-column label="应到人数" align="center" prop="toBePeople" />
      <el-table-column label="实到人数" align="center" prop="toPeople" />
      <!--<el-table-column label="会议出席人员" align="left" prop="meetingAttendees" :show-overflow-tooltip="true" />-->
      <el-table-column label="所属场站" align="left" prop="stationName" width="300" v-if="!station_btn"/>
      <!--<el-table-column label="会议内容" align="center" prop="meetingContent" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:meeting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:meeting:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改安全生产领导小组会议记录台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会议名称" prop="meetingName">
              <el-input v-model="form.meetingName" placeholder="请输入会议名称" maxlength="30"  show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议时间" prop="meetingTime">
              <el-date-picker clearable size="small" style="width: 100%"
                v-model="form.meetingTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择会议时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议地点" prop="meetingPlace">
              <el-input v-model="form.meetingPlace" placeholder="请输入会议地点" maxlength="50"  show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主持人" prop="meetingHost">
              <el-input v-model="form.meetingHost" placeholder="请输入主持人" maxlength="15"  show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="记录人" prop="meetingNoteTaker">
              <el-input v-model="form.meetingNoteTaker" placeholder="请输入记录人" maxlength="15"  show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应到人数" prop="toBePeople">
              <el-input-number v-model="form.toBePeople" style="width: 100%" placeholder="请输入应到人数" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实到人数" prop="toPeople">
              <el-input-number v-model="form.toPeople" style="width: 100%" placeholder="请输入实到人数" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议出席人员" prop="meetingAttendees">
              <el-input v-model="form.meetingAttendees" type="textarea" placeholder="请输入会议出席人员" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会议内容" prop="meetingContent">
              <el-input v-model="form.meetingContent" type="textarea" placeholder="请输入会议内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上传图片" prop="createBy">
              <el-upload
                :multiple="true"
                accept=".jpg, .png, .gif, .bmp, .jpeg"
                :headers="upload.headers"
                :action="upload.url"
                :file-list="form.imgList"
                list-type="picture-card"
                :limit="upload.limit"
                :before-upload="beforeImageUpload"
                :on-exceed="exceedFile"
                :on-success="handlePictureSuccess"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove">
                <i class="el-icon-plus"></i>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png/bmp/jpeg文件，且不超过10M</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="openView" width="800px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5" style="float:right;">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleViewExport(id)"
          >导出</el-button>
          <el-button
            type="primary"
            icon="el-icon-printer"
            size="mini"
            @click="printPreview()"
          >打印</el-button>
        </el-col>
      </el-row>
      <el-form ref="form" id="print" :model="form" :rules="rules" label-width="120px">
        <div class="div-view">
          <h2>道路货运站（场）安全生产领导小组会议记录</h2>
          <el-row :gutter="10" class="mb8">
            <span style="float: right;padding-right:10px;line-height: 28px;">{{form.meetingTime | dateString('YYYY 年 MM 月 DD 日') }} &nbsp;&nbsp; {{form.meetingWeek}}</span>
          </el-row>
          <table class="sheet_table">
            <tr>
              <td width="15%;" rowspan="2">会议时间</td>
              <td width="20%;" rowspan="2">{{form.meetingTime | dateString('YYYY-MM-DD HH:mm') }}</td>
              <td width="15%;" rowspan="2">会议地点</td>
              <td width="20%;" rowspan="2">{{form.meetingPlace}}</td>
              <td width="15%;">主持人</td>
              <td width="15%;">{{form.meetingHost}}</td>
            </tr>
            <tr>
              <td>记录人</td>
              <td>{{form.meetingNoteTaker}}</td>
            </tr>
            <tr>
              <td rowspan="2">会议名称</td>
              <td colspan="3" rowspan="2" style="text-align: left;">{{form.meetingName}}</td>
              <td>应到人数</td>
              <td>{{form.toBePeople}}</td>
            </tr>
            <tr>
              <td>实到人数</td>
              <td>{{form.toPeople}}</td>
            </tr>
            <tr>
              <td>出席人（签到）</td>
              <td colspan="5" style="text-align: left;">{{form.meetingAttendees}}</td>
            </tr>
            <tr>
              <td colspan="6">会 议 主 要 内 容 记 录</td>
            </tr>
            <tr>
              <td colspan="6" style="text-align: left;">
                <div style="min-height: 100px;">{{form.meetingContent}}</div>
              </td>
            </tr>
            <tr v-if="form.imgList && form.imgList.length > 0 ">
              <td colspan="6">照 片</td>
            </tr>
            <tr v-if="form.imgList && form.imgList.length > 0 ">
              <td colspan="6" style="text-align: left;">
                <ul>
                  <li v-for="img in form.imgList" style="text-align: center;">
                    <el-image :preview-src-list="srcList" :src="img.url" style="width: 100px;height:100px;"></el-image>
                  </li>
                </ul>
              </td>
            </tr>
          </table>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMeeting, getMeeting, delMeeting, addMeeting, updateMeeting, exportMeeting, exportMeetingDetail } from "@/api/ledger/meeting";
import { listStation} from "@/api/weight/station";
import { getToken } from "@/utils/auth";
import { getLodop } from '@/utils/C-Lodop/LodopFuncs';
export default {
  name: "Meeting",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 安全生产领导小组会议记录台账表格数据
      meetingList: [],
      stationList: [],
      dateRange: [],
      // 弹出层标题
      title: "",
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        //  文件个数
        limit: 5
      },
      // 是否显示弹出层
      open: false,
      openView: false,
      station_btn: false,
      tableHeight: window.innerHeight - 275,
      srcList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: undefined,
        stationName: undefined,
        meetingTime: undefined,
        meetingName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        meetingTime: [
          { required: true, message: "会议时间不能为空", trigger: "blur" }
        ],
        meetingPlace: [
          { required: true, message: "会议地点不能为空", trigger: "blur" }
        ],
        meetingName: [
          { required: true, message: "会议名称不能为空", trigger: "blur" }
        ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
    };
  },
  created() {
    const stationId = this.$route.params && this.$route.params.stationId;
    this.queryParams.stationId=stationId;
    const stationName = this.$route.params && this.$route.params.stationName;
    this.queryParams.stationName=stationName;
    this.getList();
    this.getStationList();

    const roles = this.$store.getters && this.$store.getters.roles;
    for (let rol of roles){
      if ('station'==rol || 'hs_station'==rol){
        this.station_btn=true;
      }
    }
  },
  methods: {
    /** 查询安全生产领导小组会议记录台账列表 */
    getList() {
      this.loading = true;
      listMeeting(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.meetingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 加载场站
    getStationList() {
      let _param={
        pageNum: 1,
        pageSize: 10000,
        stationName: undefined,
        stationCode: undefined,
      }
      listStation(_param).then(response => {
          this.stationList = response.rows;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationId: undefined,
        meetingTime: undefined,
        meetingWeek: undefined,
        meetingPlace: undefined,
        meetingHost: undefined,
        meetingNoteTaker: undefined,
        toBePeople: undefined,
        toPeople: undefined,
        meetingName: undefined,
        meetingAttendees: undefined,
        meetingContent: undefined,
        delFlag: undefined,
        createTime: undefined,
        createId: undefined,
        updateTime: undefined,
        updateId: undefined,
        remark: undefined,
        createBy: undefined,
        imgList: [],
        imgs:[],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全生产领导小组会议记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMeeting(id).then(response => {
        this.form = response.data;
        const imgs = [];
        this.form.imgList.map(function (item) {
          imgs.push(item.url);
        });
        this.form.imgs = imgs;
        this.open = true;
        this.title = "修改安全生产领导小组会议记录";
      });
    },
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getMeeting(id).then(response => {
        this.id = row.id;
        this.form = response.data;
        const imgs = [];
        this.form.imgList.map(function (item) {
          imgs.push(item.url);
        });
        this.srcList = imgs;
        this.openView = true;
        this.title = "安全生产领导小组会议记录详情";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMeeting(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addMeeting(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该条安全生产领导小组会议记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMeeting(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有安全生产领导小组会议记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMeeting(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 导出详情操作 */
    handleViewExport(id) {
      this.$confirm('是否确认导出该条安全生产领导小组会议记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMeetingDetail(id);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    beforeImageUpload(file){
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
        return false
      }
    },
    exceedFile(files, fileList) {
      this.$notify.warning({
        title: '警告',
        message: `最多只能上传${this.upload.limit}个图片`
      });
    },
    //图片上传
    handleRemove(file, fileList) {
      this.form.imgs.splice(this.form.imgs.indexOf(file.url),1);
      // this.form.imgList.splice(this.form.imgList.indexOf(file.url),1);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handlePictureSuccess(response, file) {
      this.form.imgs.push(response.fileName);
      // this.form.imgList.push({
      //   url: response.fileName
      // });
    },
    // 打印
    printPreview() {
      this.CreateOneFormPage()
      // LODOP.PRINT() //打印预览
      LODOP.PREVIEW() //打印预览
    },
    CreateOneFormPage() {
      LODOP = getLodop()
      //样式
      var body = this.global.meeting_style +'<body>' + document.getElementById('print').innerHTML + '</body>'
      // LODOP.PRINT_INIT('安全生产领导小组会议记录台账详情') //打印初始化
      // LODOP.SET_PRINT_STYLE('FontSize', 18) //设置对象风格
      // LODOP.SET_PRINT_STYLE('Bold', 1) ////设置对象风格
      // LODOP.ADD_PRINT_TEXT(50, 521, 130, 39, this.description) //增加纯文本项
      // LODOP.SET_PRINT_PAGESIZE(0, 2000, 2000, '') //设定纸张大小
      // LODOP.SET_PRINT_MODE('PRINT_PAGE_PERCENT', '55%')//设置缩放
      // LODOP.SET_PREVIEW_WINDOW(2, 2, 0, 0, 0, '')//设置窗口
      // LODOP.ADD_PRINT_HTM(88, 20, 2000, 2000, body) //增加超文本项
      LODOP.SET_PRINT_PAGESIZE(2, 0, 0, "A4");
      LODOP.SET_PRINT_STYLE("FontSize", 12);
      LODOP.ADD_PRINT_HTM(5, 5, "100%", "100%", body);
      LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);//横向时的正向显示
    }
  }
};
</script>
<style lang="scss" scoped>
  @import '../../../assets/styles/ledger_style.scss';
</style>
